export const minxi = {
  data () {
    return {
      selection: []
    }
  },
  methods: {
    publicDelete (params) {
      this.$confirm('此操作将永久删除该项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const id = params[this.idName]
        this.$Ajax(
          `${this.action.removeDataByKey}${id}`,
          'delete',
          {},
          (res) => {
            console.log(res)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.pageListName && this[this.pageListName] && this[this.pageListName]()
          }
        )
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
     * 表格选项发生变化时触发
     * @param selection 选中的选项
     */
    publicSelectionChange (selection) {
      console.log(selection)
      this.selection = selection
    },
    /**
     * 批量删除
     */
    publicDeleteBatch () {
      const selection = this.selection
      if (selection.length <= 0) {
        this.$message.warning('请先选择一个操作项')
      } else {
        this.$confirm(`此操作将删除${selection.length}条数据，你确定还要继续吗？`, '你确定要删除吗？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (!this.action.deletes) {
            throw new Error('删除失败')
          } else {
            // eslint-disable-next-line no-unused-vars
            let ids = ''
            selection.forEach(item => {
              ids += item[this.idName] + ';'
            })
            // 截取字符串
            ids = ids.substring(0, ids.length - 1)
            console.log(ids)
            this.$Ajax(
              `${this.action.deletes}${ids}`,
              'delete',
              {},
              (res) => {
                console.log(res)
                this.$message.success('删除成功!')
                this.getPageList()
              }
            )
          }
        }).catch(() => {
          this.$message.info('已取消删除')
        })
      }
    }
  }
}